﻿using System.ComponentModel;
using System.Windows.Threading;
using WMPLib;

namespace RadioPlayer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    using MahApps.Metro.Controls;

    /// <summary>
    /// Interaktionslogik für DialogWindow.xaml
    /// </summary>
    public partial class DialogWindow : MetroWindow
    {
        #region Fields

        private RPlayer _player;

        #endregion Fields

        #region Constructors

        public DialogWindow(RPlayer player)
        {
           
            InitializeComponent();
            _player = player;
        }

        #endregion Constructors

        #region Properties

        public RadioStream Stream
        {
            get; set;
        }

        #endregion Properties

        #region Methods

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var button = MyVisualTreeHelper.FindChild<Button>(this, "btnMax");
            button.Visibility = Visibility.Hidden;
            var button2 = MyVisualTreeHelper.FindChild<Button>(this, "btnMin");
            button2.Visibility = Visibility.Hidden;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(tbStreamName.Text) && !String.IsNullOrEmpty(tbStreamUrl.Text))
            {
                Stream = new RadioStream {StreamName = tbStreamName.Text, StreamUri = tbStreamUrl.Text, Id = new Guid()};
                _player.RadioStreams.Add(Stream);

                DialogResult = true;
                this.Close();
            }

            else{MessageBox.Show("Please fill out name and URL.");}
        }

        #endregion Methods

        private void TestButton_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(tbStreamUrl.Text))
            {
                IsBeingTested = true;
                TestButton.IsEnabled = false;
                string url = tbStreamUrl.Text;

                wmp = new WindowsMediaPlayer();
                wmp.PlayStateChange += PlayerStateChanged;
                wmp.MediaError += OnMediaError;
                wmp.settings.volume = 0;
                wmp.URL = url;

            }

            else {
                MessageBox.Show("Url must not be empty."); }
        }

        public bool IsBeingTested { get; set; }

        private WindowsMediaPlayer wmp { get; set; }

        void PlayerStateChanged(int newState)
        {
            switch((WMPPlayState) newState)
            {
                case WMPPlayState.wmppsPlaying:
                    LinkStatusLabel.Text = "URL is valid!";
                    LinkStatusLabel.Foreground = Brushes.Green;
                    wmp.controls.stop();
                    TestButton.IsEnabled = true;
                    IsBeingTested = false;
                    break;
                case WMPPlayState.wmppsStopped:

                    break;
                case WMPPlayState.wmppsReady:
                    if (IsBeingTested)
                    {
                        LinkStatusLabel.Text = "URL is invalid!";
                        LinkStatusLabel.Foreground = Brushes.Red;
                        TestButton.IsEnabled = true;
                        IsBeingTested = false;
                    }
                    break;
                    
            }
        }

        void OnMediaError(object ob)
        {
            LinkStatusLabel.Text = "URL is invalid!";
            LinkStatusLabel.Foreground = Brushes.Red;
            TestButton.IsEnabled = true;
            IsBeingTested = false;
        }

        private void tbStreamUrl_TextChanged(object sender, TextChangedEventArgs e)
        {
            LinkStatusLabel.Text = "URL not tested.";
            LinkStatusLabel.Foreground = Brushes.Black;
        }

       }
}